//
//  MLAOrderFoodHomeVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/30.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAOrderFoodHomeVC.h"
#import "MLABaseSearchView.h"
#import "MLAFoodTypeListModel.h"
#import "MLAFoodHomeListModel.h"
#import "MLAFoodHomeListCell.h"
#import "MLAShopFoodsListVC.h"
#import "MLAFoodSearchVC.h"

@interface MLAOrderFoodHomeVC ()

@property (nonatomic, strong) NSMutableArray<MLAFoodTypeListModel *> *typeListModels;
@property (nonatomic, strong) NSMutableArray<MLAFoodHomeListModel *> *shopListModels;


@end

@implementation MLAOrderFoodHomeVC {
    MLABaseSearchView *searchView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    lineView.hidden = YES;
    [self configTitle:@"就餐方"];
    
    searchView = [[MLABaseSearchView alloc] init];
    searchView.canEdit = NO;
    [self.view addSubview:searchView];
    [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(self.titleView.mas_bottom);
        make.height.mas_equalTo(80);
    }];
    
    self.haveRefreshHeader = YES;
    self.tableView.rowHeight = 68;
    [self.tableView registerNib:[UINib nibWithNibName:@"MLAFoodHomeListCell" bundle:nil] forCellReuseIdentifier:kCellID];
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(self->searchView.mas_bottom);
    }];
    
    @weakify(self);
    searchView.shouldEditingBlock = ^{
        @strongify(self);
        [self gotoSearchVC];
    };
    

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (!self.tableView.tableHeaderView) {
        [self loadTypeListData];
    }
    [self loadData];
}

- (void)loadTypeListData {
    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:productTypeList params:@{@"type":@"2"} success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        [self typeListView:response[@"lists"]];
    } failure:^{
        
    }];
}

- (void)loadData {
    
    [super loadData];

    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:kShopList params:@{@"num":@"100"} success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self.shopListModels = [NSMutableArray array];
        self.shopListModels = [MLAFoodHomeListModel mj_objectArrayWithKeyValuesArray:response[@"lists"]];
        [self.tableView reloadData];
    } failure:^{
        
    }];
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _shopListModels.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MLAFoodHomeListCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID forIndexPath:indexPath];
    cell.model = _shopListModels[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MLAFoodHomeListModel *model = _shopListModels[indexPath.row];
    MLAShopFoodsListVC *sflVC = [[MLAShopFoodsListVC alloc ] init];
    sflVC.model = model;
    [self.navigationController pushViewController:sflVC animated:YES];
}

#pragma mark - 创建分类视图
- (void)typeListView:(NSArray *)list {
    
    self.typeListModels = [NSMutableArray array];
    self.typeListModels = [MLAFoodTypeListModel mj_objectArrayWithKeyValuesArray:list];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CX_GetScreenWidth(), CX_GetXScale(165)+30+40)];
    self.tableView.tableHeaderView = headerView;
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(0);
        make.width.offset(CX_GetScreenWidth());
        make.height.offset(CX_GetXScale(165)+30+40);
    }];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.frame = CGRectMake(0,0,CX_GetXScale(345),CX_GetXScale(165));
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.shadowColor = CX_GetColorHex(0x5C9EEB, 0.3).CGColor;
    bgView.layer.shadowOffset = CGSizeMake(0, 1);
    bgView.layer.shadowOpacity = 1;
    bgView.layer.cornerRadius = 5;
    [headerView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(CX_GetXScale(15));
        make.right.offset(-CX_GetXScale(15));
        make.height.offset(CX_GetXScale(165));
        make.top.offset(15);
    }];

    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = CX_GetColorHex(0x5C9EEB, 0.1);
    [bgView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.left.right.offset(0);
        make.height.offset(1);
    }];
    
    for (int i = 0; i < 3; i++) {
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = CX_GetColorHex(0x5C9EEB, 0.1);
        [bgView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.offset(0);
            make.width.offset(1);
            make.left.offset(CGRectGetWidth(bgView.frame)/4.*(i+1));
        }];
    }
    
    NSMutableArray *btnsArr = [NSMutableArray array];

    [self.typeListModels enumerateObjectsUsingBlock:^(MLAFoodTypeListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIControl *ctrl = [[UIControl alloc] init];
        ctrl.frame = CGRectMake(CGRectGetWidth(bgView.frame)/4.*(idx%4), CGRectGetHeight(bgView.frame)/2.*(idx/4), CGRectGetWidth(bgView.frame)/4., CGRectGetHeight(bgView.frame)/2.);
        ctrl.tag = 12000+idx;
        [ctrl addTarget:self action:@selector(typeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:ctrl];
        [btnsArr addObject:ctrl];
        
        UIImageView *iconImgv = [[UIImageView alloc] init];
        [iconImgv sd_setImageWithURL:[NSURL URLWithString:obj.imgurl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            [iconImgv mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.offset(0);
                make.top.offset(20);
                make.width.offset(image.size.width/4.);
                make.height.offset(image.size.height/4.);
            }];
        }];
        [ctrl addSubview:iconImgv];
        
        UILabel *titleLbl = [[UILabel alloc] init];
        titleLbl.textColor = CX_GetColorSubTitle();
        titleLbl.text = obj.name;
        titleLbl.textAlignment = NSTextAlignmentCenter;
        titleLbl.font = CX_GetNormalFont(12);
        [ctrl addSubview:titleLbl];
        [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.offset(0);
            make.bottom.offset(-20);
        }];
    }];

    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.text = @"店铺";
    titleLbl.font = CX_GetNormalFont(18);
    titleLbl.textColor = CX_GetColorTitle();
    [headerView addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.bottom.offset(-10);
    }];
}


#pragma mark - Action
- (void)typeBtnAction:(UIControl *)ctrl {
    NSInteger index = ctrl.tag - 12000;
    
    MLAFoodTypeListModel *model = _typeListModels[index];
    
    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:kShopList params:@{@"num":@"100",@"shop_type":model.modelId} success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self.shopListModels = [NSMutableArray array];
        self.shopListModels = [MLAFoodHomeListModel mj_objectArrayWithKeyValuesArray:response[@"lists"]];
        [self.tableView reloadData];
    } failure:^{
        
    }];

}




- (void)gotoSearchVC {
    MLAFoodSearchVC *fsVC = [[MLAFoodSearchVC alloc] init];
    [self.navigationController pushViewController:fsVC animated:YES];
}

@end
